const Router = require('koa-router');
const router = new Router();
const tool = require('../../tools/tool')
const {db, QueryTypes} = require("../../models/db");
const tools = require("../../tools/tool");

router.post('/home/top', async ctx => {
    try {
        let tenantId = ctx.user.tenantId;
        let body = ctx.request.body;
        let billType = body.billType
        let range = body.range
        let billState = ''

        if(billType && range) {

            let beginDate = ''
            let endDate = ''
            if(range === '近一周'){
                endDate = tool.getDay('')
                beginDate = tool.getDay('', -7)
            }
            if(range === '近30天'){
                endDate = tool.getDay('')
                beginDate = tool.getDay(tool.dayjs().subtract(1, 'month'))
            }
            if(range === '近半年'){
                endDate = tool.getDay('',  )
                beginDate = tool.getDay(tool.dayjs().subtract(6, 'month'))
            }
            if(range === '近一年'){
                endDate = tool.getDay('',  )
                beginDate = tool.getDay(tool.dayjs().subtract(1, 'year'))
            }
            let sql = ''
            if(['待审核','已审核','已删除'].indexOf(billType)!==-1) {
                let billState = ''
                if(billType === '待审核') {
                    billState = ` and billState='draft' `
                }
                if(billType === '已审核') {
                    billState = ` and billState='accounted' `
                }
                if(billType === '已删除') {
                    billState = ` and billState='del' `
                }
                sql =`select transType,count(*) total from
                                    invoice where tenantId=${tenantId} and billType in('PUR','SALE','RECEIPT','PAYMENT') and billDate between '${beginDate}' and '${endDate}' 
                                            ${billState} 
                                            group by transType `
            }
            else {
                sql =`select transType,count(*) total from
                                    v_main_withhx where tenantId=${tenantId} and billType in('PUR','SALE') and billDate between '${beginDate}' and '${endDate}' 
                                            and billState='accounted' and arrearsReal<>hxAmount 
                                            group by transType `
            }
            let list =  await db.query(sql, {type: QueryTypes.SELECT})
            list.map(row => {
                if(row.transType === 150601) {
                    row.text ='销售单'
                    row.route = 's-in'
                }
                if(row.transType === 150602) {
                    row.text ='销售退货单'
                }
                if(row.transType === 150603) {
                    row.text ='销售换货单'
                }
                if(row.transType === 150501) {
                    row.text ='购货单'
                }
                if(row.transType === 150502) {
                    row.text ='购货退货单'
                }
                if(row.transType === 153001) {
                    row.text ='付款单'
                }
                if(row.transType === 153101) {
                    row.text ='收款单'
                }
            })
            return ctx.suc('', list)
        }
        else {
            return ctx.err('没有选择条件')
        }

    } catch (e) {
        return ctx.err(e);
    }
})


module.exports = router;
